<template>
    <!-- 新增楼层 -->
    <el-dialog v-model="isShow" title="新增楼层库位码" width="527">
        <el-form ref="ruleFormRef" :model="formData" :rules="rules" label-width="120px" class="demo-ruleForm">
            <el-form-item label="货架类型" prop="shelvesType">
                <el-select v-model="formData.shelvesType" placeholder="请选择货架类型">
                    <el-option v-for="item in hm_stock_shelves_type" :key="item.value" :label="item.label"
                        :value="item.value" />
                </el-select>
            </el-form-item>
            <el-form-item label="楼层" prop="floor">
                <el-input-number v-model="formData.floor" style="width: 214px;" min="1" max="99" />
            </el-form-item>
            <!-- <el-form-item label="区域" prop="area">
                <el-input-number v-model="formData.area" style="width: 214px;" min="1" max="99" />
            </el-form-item> -->
            <el-form-item label="排数量" prop="row">
                <el-input-number v-model="formData.row" style="width: 214px;" min="1" max="99" />
            </el-form-item>
            <el-form-item label="货架数" prop="shelves">
                <el-input-number v-model="formData.shelves" style="width: 214px;" min="1" max="99" />
            </el-form-item>
            <el-form-item label="层数量" prop="layer" v-if="formData.shelvesType == 'C01'">
                <el-input-number v-model="formData.layer" style="width: 214px;" min="1" max="99" />
            </el-form-item>
            <el-form-item label="状态" prop="status">
                <el-switch v-model="formData.status" :active-value="1" :inactive-value="2" size="large" />
            </el-form-item>
        </el-form>
        <template #footer>
            <span class="dialog-footer">
                <el-button @click="resetForm(ruleFormRef)">取消</el-button>
                <el-button type="primary" v-loading="loading" @click="submitForm(ruleFormRef)">确定</el-button>
            </span>
        </template>
    </el-dialog>
</template>
<script setup lang="ts">
import { ref, defineExpose, defineEmits } from 'vue'
import { ElMessage } from 'element-plus'
import { stockLocationGenerate } from '@/api/depot/index'

const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { hm_stock_shelves_type } = toRefs<any>(proxy?.useDict('hm_stock_shelves_type'));
const emit = defineEmits(['getTreeData'])

const loading = ref(false)
const isShow = ref(false)
const ruleFormRef = ref()
const formData = ref({
    shelvesType: '',
    floor: 1,
    // area: 1,
    row: 1,
    shelves: 1,
    layer: 1,
    status: 1,
})
// 只能输入1到99之间的整数
const validatePass = (rule: any, value: string, callback: Function) => {
    if (value) {
        return /^([1-9]|[1-9]\d)$/.test(value)
    } else {
        callback()
    }
}
// 表单验证
const rules = reactive(
    {
        shelvesType: [
            {
                required: true,
                message: '请选择货架类型',
                trigger: 'change',
            }
        ],
        floor: [
            {
                required: true,
                message: '请输入楼层数量',
                trigger: 'change',
            }, {
                validator: validatePass,
                message: '请输入1-99的整数',
                trigger: 'change'
            }
        ],
        // area: [
        //     {
        //         required: true,
        //         message: '请输入区域数量',
        //         trigger: 'change',
        //     }, {
        //         validator: validatePass,
        //         message: '请输入1-99的整数',
        //         trigger: 'change'
        //     }
        // ],
        row: [
            {
                required: true,
                message: '请输入排数量',
                trigger: 'change',
            }, {
                validator: validatePass,
                message: '请输入1-99的整数',
                trigger: 'change'
            }
        ],
        shelves: [
            {
                required: true,
                message: '请输入货架数量',
                trigger: 'change',
            }, {
                validator: validatePass,
                message: '请输入1-99的整数',
                trigger: 'change'
            }
        ],
        layer: [
            {
                required: true,
                message: '请输入层数量',
                trigger: 'change',
            }, {
                validator: validatePass,
                message: '请输入1-99的整数',
                trigger: 'change'
            }
        ],
    }
)
// 确定操作
const submitForm = async (formEl: any) => {
    if (!formEl) return
    await formEl.validate((valid: any, fields: any) => {
        if (valid) {
            apiExecute(formEl)

        } else {
            console.log('error submit!', fields)
        }
    })
}
// 表单重置
const resetForm = (formEl: any) => {
    if (!formEl) return
    formEl.resetFields()
    isShow.value = false
}

const apiExecute = async (formEl: any) => {
    loading.value = true
    try {
        const { code = 200 } = await stockLocationGenerate(formData.value)
        loading.value = false
        if (code == 200) {
            ElMessage({
                message: '新增楼层成功',
                type: 'success',
            })
            emit('getTreeData')
            resetForm(formEl)
        } else {
            ElMessage({
                message: '新增楼层失败',
                type: 'error',
            })
        }
    } catch (error) {
        loading.value = false
    }
}

defineExpose({ formData, isShow })

</script>
<style lang="scss" scoped>
@import url(../style/index.scss);
</style>